Using prefabs

Use prefabs (prefabricated templates) to structure your application and to create consistent interfaces. Prefabs allow you to create the building blocks of your application and make the application easier to maintain.

Create a prefab for every item in your application that appears more than once, when you want to logically separate a part of your application, or add content to the scene graph when your application is already running. For example, use prefabs to:

When using prefabs keep in mind that:

You can nest prefabs within prefabs.

Prefabs and prefab instances

Prefabs consume memory on the device only when you load them. When you load a prefab, the prefab consumes the amount of memory based on the number of nodes and properties that prefab contains. When you instantiate a prefab, Kanzi creates nodes and assigns properties to those nodes. You can calculate how much memory prefab instances consume by multiplying the number of instances with the memory requirements of a single prefab instance.

When you attach a node to the scene graph, resources used by the nodes are looked up and requested from the resource manager. Resource manager loads or returns existing resources if they are already loaded.

When you detach a prefab instance, Kanzi does not release resources used in the nodes you detached. This is a performance optimization, which allows you to change the parent node of the prefab, or move a prefab in the scene graph, without having to load again the resources the prefab uses. When you want to release all resources which are no longer in use, call ResourceManager::purge.

Kanzi assumes that all the nodes you place in the scene graph are going to be used. Kanzi optimizes for the case that all the content put in the scene graph is ready for immediate use. If a node is not supposed to be used, remove it from the scene graph.

Prefab Placeholder and Prefab View nodes

You can use both Prefab Placeholder and Prefab View nodes to instantiate a prefab in your project. The difference between these nodes is:

When to use a Prefab Placeholder and when a Prefab View node

Use a Prefab Placeholder node to instantiate a prefab which you do not intend to change during application runtime. If you want to dynamically change a prefab, or want to asynchronously load the resources which the prefab uses, use a Prefab View to instantiate a prefab. For example, when you theme your application and want to use different prefabs for different themes, use a Prefab View node to instantiate those prefabs.

Kanzi reads the value of the Prefab Template property of a Prefab Placeholder once, but a Prefab View node monitors for changes the value of the Prefab Template property. When the value of the Prefab Template property in a Prefab View changes, Kanzi replaces its child node with the new child node.

Creating and using a prefab

To create and use a prefab:

  1. In the Project press Alt and right-click Prefabs and select the node you want to create.
    For example, create a Button 2D node, and add an Image and a Text Block 2D nodes to it.
  2. To instantiate the prefab in your application:

Customizing instances of a prefab

A prefab can contain a tree of nodes, each with their own properties. When you edit the nodes in a prefab or any of its instances in a project, you change those nodes in all instances of that prefab. However, you can customize individual instances of the prefab to have individual values by overriding the values in the default prefab. For example, when you create a prefab for an address book entry you want to show a different name, number, and photo for each address book entry.

To customize individual instances of a prefab:

  1. In the Project select the node in a prefab you want to customize in an individual instance of that prefab.
  2. In the Properties next to the property you want to edit click .
    Kanzi Studio creates from that property a custom property, adds it to the root of the prefab, and creates a ##Template binding to the property in the prefab root. See Prefab root bindings.
  3. In the Project select the root of the prefab instance you want to customize.
  4. In the Properties click next to the property for which you enabled editing and enter the value of the property you want to use only in that instance of the prefab.

Editing a prefab

To edit a prefab double-click the prefab in the Project > Prefabs. Kanzi Studio shows only the prefab in a Preview window tab. You can now edit the prefab with the Preview tools and see the result of your edits without instantiating the prefab.

Turning scene graph nodes into prefab

To turn any part of your project's scene graph into a prefab, in the Project drag the root of the part you want to turn into a prefab to Project > Prefabs.

When you drop a node into Prefabs, Kanzi Studio creates a prefab template from the node and replaces the original node with an instance of the prefab.

Loading prefab resources asynchronously

When you load prefab resources asynchronously, Kanzi uses separate threads to load resources. When Kanzi finishes loading the resources, it deploys them using the main thread. That way your application stays responsive while your application loads resources.

To load prefab resources asynchronously:

  1. Create content you want to show while Kanzi loads prefab resources.
    For example, to create a loading screen you replace with the content of a prefab the resources of which you load asynchronously, in the Assets set Source to Factory Content, drag the Processing Indicator and drop it on the Project > Prefabs.
  2. Create a prefab the resources of which you want to load asynchronously.
    For example, in the Prefabs create a Stack Layout 2D node and add to it several Image nodes each of which points to a different image.
  3. In the Project create a Prefab View node where you want to asynchronously load prefab resources and in the Properties set the Prefab Template property to the content you want to show while Kanzi loads the resources of the prefab you created in the previous step.
    For example, set the Prefab Template property to the Processing Indicator prefab.
  4. In the Project select the Prefab View node, in the Node Components > Triggers section right-click, select Add Trigger, and select the trigger you want to use to start loading prefab resources.
    For example, add the On Attached trigger to start loading resources when Kanzi attaches the Prefab View node to the screen graph.
  5. In the Node Components click the Add dropdown menu for the trigger you added in the previous step and select the Start Asynchronous Load action.
  6. In the action settings window set the Prefab Template property to a prefab or a resource ID which points to a prefab the resources of which you want to load asynchronously.
  7. To define what happens when Kanzi finishes loading the resources:
    1. In the Project select the Prefab View node, in the Node Components > Triggers section right-click and select Add Trigger > Prefab View > Asynchronous Load Completed.
    2. In the Node Components > Triggers in the Asynchronous Load Completed trigger click the Add dropdown menu and select an action.
      For example, select the Navigate to Page action and set the Item to the Page node to which you want the application to navigate when Kanzi finishes loading the resources.
  8. In Kanzi Studio select File > Export > Export as KZB Player for Windows and open the .exe file of the application in the <ProjectName>/Tool_project/Application Player directory.
    Because the Kanzi Studio Preview loads all resources in the project when it starts, to see the effect of loading resources asynchronously, either export the project as a kzb file player, or export the project kzb file and run the application from the project Visual Studio solution.

Replacing the root node of a prefab

Replace the root node of a prefab when you want to add or change the node that is used as the parent node of a prefab. For example, if you first created a prefab that has a Grid Layout 3D as its root, but now you to place that Grid Layout 3D into a Stack Layout 3D, you need to replace the root node of that prefab.

To replace the root of a prefab:

  1. In the Project > Prefabs press Alt and right-click the root of the prefab you want to replace and select Empty Node From Items.
    Kanzi Studio replaces the current root of the prefab with an Empty Node node and places the entire prefab inside that empty node. For 3D prefabs Kanzi Studio creates an Empty Node 3D node, for 2D prefabs it creates an Empty Node 2D node.
  2. In the Project > Prefabs create the node you want to use as the new root node of the prefab.
    For example, create a Stack Layout 3D.
  3. Drag and drop the old prefab from the Empty Node node root to the new root node you created in the previous step.

Instantiating a prefab using a script

Use a script to instantiate a prefab or replace objects with prefabs in your project when you know where in the scene graph in your project you want to use a prefab, but do not want to instantiate the prefab in Kanzi Studio.

To instantiate a prefab using a script:

  1. In the Project > Prefabs create a prefab you want to instantiate using a script.
    For example, create a Text Block 3D.
  2. In the Project create the object to which you want to instantiate a prefab.
    For example, in the Scene create a Stack Layout 3D.
  3. In the Project create the object you want to use to trigger the instantiating of a prefab.
    For example, in the Scene create a Button 3D and use its Button: Click trigger.
  4. In the Node Components in the trigger you want to use instantiate a prefab, click the Add dropdown menu, and select the Execute Script action.
    The Execute Script window opens.
  5. In the Execute Script window select an existing script, or select + Create Script to create a new script.
    The Script Editor window opens.
  6. In the Script Editor write the script that instantiates a prefab. See Scripting reference.
    To get the kzb URL of a prefab, in the Project right-click the prefab and select Copy .kzb URL.
    For example, to instantiate the Text Block 3D prefab in a Stack Layout 3D using the Button 3D you created in this procedure
    var layout = node.lookupNode('../Stack Layout 3D');
    var item = instantiatePrefab('kzb://ProjectName/Prefabs/Text Block 3D', 'Text prefab');
    layout.addChild(item);
  7. When you are done writing the script, in the Script Editor click Save.
  8. Set off the trigger to execute the script.
    For example, if you created a button and used the Button: Click trigger, click that button.

Using the Prefab View 3D node in the API

To use a Prefab view 3D node, create a prefab with 3D content and set it using `setPrefab()` :

// Create a Prefab view 3D and add it to the scene.
PrefabView3DSharedPtr prefabView3D = PrefabView3D::create(domain, "Prefab view");
scene->addChild(prefabView3D);

// Acquire prefab from resource manager.
PrefabTemplateSharedPtr prefabTemplate = resourceManager->acquireResource<PrefabTemplate>("kzb://my_project/Prefabs/My3DPrefab");

// Use the prefab in the PrefabView3D node.
prefabView3D->setPrefab(prefabTemplate);

For details, see the PrefabView3D class in the API reference.

Using the Prefab View 2D node in the API

To use a Prefab view 2D node, create a prefab with 2D content and set it using `setPrefab()` :

// Create a Prefab view 2D and add it to the viewport.
PrefabView2DSharedPtr prefabView2D = PrefabView2D::create(domain, "Prefab view");
viewportNode->addChild(prefabView2D);

// Acquire prefab from resource manager.
PrefabTemplateSharedPtr prefabTemplate = resourceManager->acquireResource<PrefabTemplate>("kzb://my_project/Prefabs/My2DPrefab");

// Use the prefab in the PrefabView2D node.
prefabView2D->setPrefab(prefabTemplate);

For details, see the PrefabView2D class in the API reference.

See also

Using scripts

Scripting reference